package it.asvp.asvp_portal.data.dao.impl;


import it.asvp.asvp_portal.data.dao.CouncilDao;
import it.asvp.asvp_portal.data.entity.member.Council;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class CouncilDaoImpl implements CouncilDao {

	private static final Log log = LogFactory.getLog(CouncilDaoImpl.class);

	@Autowired
	SessionFactory sessionFactory;

	public Long persist(Council transientInstance) {
		log.debug("persisting Council instance");
		Long id;
		try {
			id=(Long) sessionFactory.getCurrentSession().save(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
		return id;
	}

	public void delete(Council persistentInstance) {
		log.debug("deleting Council instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Council merge(Council detachedInstance) {
		log.debug("merging Council instance");
		try {
			Council result = (Council) sessionFactory.getCurrentSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	 public Council findById(java.lang.Long id) {
			
			Criteria cr=sessionFactory.getCurrentSession().createCriteria(Council.class);
			cr.add(Restrictions.eq("id", id));
			Council c = (Council)cr.uniqueResult();
			return c;
		}
	 
	 @SuppressWarnings("unchecked")
	public List<Council> findByYear(Integer startYear,Integer endYear){
			
			Criteria cr=sessionFactory.getCurrentSession().createCriteria(Council.class);
			cr.add(Restrictions.eq("startYear", startYear));
			cr.add(Restrictions.eq("endYear", endYear));
			List<Council> list = (List<Council>)cr.list();
			return list;
	 }
	 
	 @SuppressWarnings("unchecked")
	 public List<Council> getCurrent() {
			
			Criteria cr=sessionFactory.getCurrentSession().createCriteria(Council.class);
			cr.add(Restrictions.eq("current", true));
			 List<Council> list = (List<Council>)cr.list();
			return list;
		}
	 
}
